import java.util.Iterator;
import java.util.concurrent.ConcurrentHashMap;

public final class Mappings {

	private static final ConcurrentHashMap<String, VSensorConfig> vsNameTOVSConfig = new ConcurrentHashMap<String, VSensorConfig>();

	private static final ConcurrentHashMap<String, VirtualSensor> fileNameToVSInstance = new ConcurrentHashMap<String, VirtualSensor>();

	public static boolean addVSensorInstance(VirtualSensor sensorPool) {
		vsNameTOVSConfig.put(sensorPool.getConfig().getName(),
				sensorPool.getConfig());
		//System.out.println(sensorPool.getConfig().getFileName() + " is added to the system!");
		fileNameToVSInstance.put(sensorPool.getConfig().getFileName(),
				sensorPool);
		return true;
	}

	public static VirtualSensor getVSensorInstanceByFileName(String fileName) {
		return fileNameToVSInstance.get(fileName);
	}

	//ignore capital case
	public static VSensorConfig getVSensorConfig(String vSensorName) {
		if (vSensorName == null)
			return null;
		Iterator<VSensorConfig> configs = Mappings.getAllVSensorConfigs();
		while (configs.hasNext()) {
			VSensorConfig config = configs.next();
			if (config.getName().equalsIgnoreCase(vSensorName))
				return config;
		}
		return null;
	}

	public static void removeConfigByFileName(String fileName) {
		if (fileNameToVSInstance.containsKey(fileName)) {
			VSensorConfig config = (fileNameToVSInstance.get(fileName))
					.getConfig();
			vsNameTOVSConfig.remove(config.getName());
			fileNameToVSInstance.remove(fileName);
		}
	}

	public static Long getLastModifiedTime(String configFileName) {
		return Long.valueOf((fileNameToVSInstance.get(configFileName))
				.getLastModified());
	}

	public static String[] getAllKnownFileName() {
		return fileNameToVSInstance.keySet().toArray(new String[0]);
	}

	public static VSensorConfig getVSensorConfigByFileName(String fileName) {
		if (fileName == null)
			return null;
		return (fileNameToVSInstance.get(fileName)).getConfig();
	}

	public static Iterator<VSensorConfig> getAllVSensorConfigs() {
		return vsNameTOVSConfig.values().iterator();
	}

	public static VirtualSensor getVSensorInstanceByVSName(String vsensorName) {
		if (vsensorName == null)
			return null;
		VSensorConfig vSensorConfig = vsNameTOVSConfig.get(vsensorName);
		if (vSensorConfig == null)
			return null;
		return getVSensorInstanceByFileName(vSensorConfig.getFileName());
	}

}
